import javax.swing.JFrame;
import cii_lib.JSserver;
import cii_lib.JSserverException;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; 
import java.io.*;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class VME_AccessMode extends AccessMode {
	private static final long serialVersionUID = 1L;
	private JSserver jserv;
	private String address;
	private int port;
	private int step;
	
	private void loadConfig() {
        try {
        		BufferedReader rdr=new BufferedReader(new FileReader(new File("VME_AccessMode.config")));
                address=rdr.readLine();
                port=new Integer(rdr.readLine());
                step=new Integer(rdr.readLine());
               
        } catch (Exception e) { saveConfig(); }
	};
	
	private void saveConfig() {
		FileOutputStream out;
        PrintStream p;
        try {
                out = new FileOutputStream("VME_AccessMode.config");
                p = new PrintStream( out );
                p.println (address);
                p.println (port);
                p.println (step);
                p.close();
        } catch (Exception e) { e.printStackTrace(); }
	};
	
	public VME_AccessMode() 	{
		step = 1;
		address = "localhost";
		port = 8011;
	}
	
	public void Init() {
		loadConfig(); 
		jserv=new JSserver(step);
		}
	
	public void Configure(String key, String value) {	}

	public void Connect() throws AccessModeException {
		try {
			jserv.Open(address,port);
		} catch (JSserverException e) {
			throw new AccessModeException(e.getMessage());
		}
	}

	public void Disconnect() throws AccessModeException {
		try {
			jserv.Close();
		} catch (JSserverException e) {
			throw new AccessModeException(e.getMessage());
		}
	}
	
	public String getDescription() {
		return "przez internet";
	}

	public String getModeName() {
		return "VME (JSserver)"; 
	}

	public void Write(long address, long data) throws AccessModeException {
		try {
			jserv.Write(address, data);
		} catch (JSserverException e) {
			throw new AccessModeException(e.getMessage());
		}
	}
	
	public long Read(long address) throws AccessModeException {
		try {
			return jserv.Read(address);
		} catch (JSserverException e) {
			e.printStackTrace();
			throw new AccessModeException(e.getMessage());
		}
	}

	public void configDialog(JFrame parentFrame) {
		VME_AccessMode_cfg tmp=new VME_AccessMode_cfg();
		tmp.initGUI();
		tmp.setVisible(true);
		tmp.setModal(true);
	}
	
	private class VME_AccessMode_cfg extends JDialog
	{
		private static final long serialVersionUID = 1L;
		private JPanel jPanel1;
		private JButton jButton1;
		private JButton jButton3;
		private JTextField jTextField3;
		private JLabel jLabel3;
		private JButton jButton2;
		private JTextField jTextField2;
		private JLabel jLabel2;
		private JLabel jLabel1;
		private JPanel jPanel2;
		private JTextField jTextField1;
		
		VME_AccessMode_cfg()
		{
			
		}
		
		private void initGUI() {
			try {
				{
					final JDialog thisDialog=this;
					this.setTitle("VME(JServer) Access Mode Configuration Dialog ");
					this.setAlwaysOnTop(true);
					this.setResizable(false);
					{
						jPanel1 = new JPanel();
						getContentPane().add(jPanel1, BorderLayout.CENTER);
						jPanel1.setPreferredSize(new java.awt.Dimension(265, 336));
						jPanel1.setLayout(null);
						{
							jPanel2 = new JPanel();
							jPanel1.add(jPanel2);
							jPanel2.setBounds(12, 12, 284, 281);
							jPanel2.setBorder(BorderFactory.createTitledBorder("Settings"));
							jPanel2.setLayout(null);
							{
								jLabel1 = new JLabel();
								jPanel2.add(jLabel1);
								jLabel1.setText("Address");
								jLabel1.setBounds(17, 27, 68, 14);
							}
							{
								jTextField1 = new JTextField();
								jPanel2.add(jTextField1);
								jTextField1.setText(address);
								jTextField1.setBounds(85, 24, 141, 21);
							}
							{
								jLabel2 = new JLabel();
								jPanel2.add(jLabel2);
								jLabel2.setText("Port");
								jLabel2.setBounds(17, 53, 68, 14);
							}
							{
								jTextField2 = new JTextField();
								jPanel2.add(jTextField2);
								jTextField2.setText(Integer.toString(port));
								jTextField2.setBounds(85, 50, 57, 21);
							}
							{
								jLabel3 = new JLabel();
								jPanel2.add(jLabel3);
								jLabel3.setText("Step");
								jLabel3.setBounds(17, 79, 56, 14);
							}
							{
								jTextField3 = new JTextField();
								jPanel2.add(jTextField3);
								jTextField3.setText(Integer.toString(step));
								jTextField3.setBounds(85, 76, 57, 21);
							}

						}
						{
							jButton1 = new JButton();
							jPanel1.add(jButton1);
							jButton1.setText("Cancel");
							jButton1.setBounds(221, 299, 76, 25);
							jButton1.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) {
									thisDialog.dispose();
									
								}
								});
						}
						{
							jButton3 = new JButton();
							jPanel1.add(jButton3);
							jButton3.setText("Save as Default");
							jButton3.setBounds(20, 299, 130, 25);

							jButton3.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) {
									address=jTextField1.getText();
									port= new Integer(jTextField2.getText());
									step=new Integer(jTextField3.getText());
									saveConfig();
									thisDialog.dispose();
									Init();
								}
								});
						}
						{
							jButton2 = new JButton();
							jPanel1.add(jButton2);
							jButton2.setText("Ok");
							jButton2.setBounds(166, 299, 50, 25);

							jButton2.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) {
									address=jTextField1.getText();
									port= new Integer(jTextField2.getText());
									step=new Integer(jTextField3.getText());
									thisDialog.dispose();
									Init();
								}
								});
						}
					}
				}
				this.setSize(314, 367);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

